{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "74b10c93",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b250ab19",
   "metadata": {},
   "source": [
    "# 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9d04a9df",
   "metadata": {},
   "outputs": [],
   "source": [
    "score = np.array([[38, 21, 45, 12, 3], [17, 42, 29, 8, 46],\n",
    "                  [25, 11, 37, 0, 19], [41, 2, 15, 33, 27],\n",
    "                  [9, 48, 13, 30, 7], [22, 40, 1, 35, 18]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a3fcadd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[38, 21, 45, 12,  3],\n",
       "       [17, 42, 29,  8, 46],\n",
       "       [25, 11, 37,  0, 19],\n",
       "       [41,  2, 15, 33, 27],\n",
       "       [ 9, 48, 13, 30,  7],\n",
       "       [22, 40,  1, 35, 18]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "630d6f24",
   "metadata": {},
   "source": [
    "# 效率对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5d75a8c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 774 ms\n",
      "Wall time: 165 ms\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "\n",
    "#通过;time魔法方法,查看当前行的代码运行一次所花费的时间\n",
    "%time sum1=sum(a)\n",
    "\n",
    "b=np.array(a)\n",
    "\n",
    "%time sun2=np.sum(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0504e746",
   "metadata": {},
   "source": [
    "# ndarray的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b45e95cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 5)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.shape # 数组维度的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "55638f21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.ndim # 数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e0ff2aef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.size # 数组中的元素数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bfe8e4da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.itemsize # 一个数组元素的产长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "49e89832",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.dtype # 数组元素的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f79d1b4a",
   "metadata": {},
   "source": [
    "# naarray的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c9322659",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4e47a327",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([[1,2,3],[1,2,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2c941995",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[7,7,9]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a7e6429f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "53181819",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 4]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a1b36f27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3],\n",
       "        [4, 5, 6]],\n",
       "\n",
       "       [[1, 2, 3],\n",
       "        [7, 7, 9]]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a0a6a2b",
   "metadata": {},
   "source": [
    "# ndarray的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "99623745",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.array([1.2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c754ef1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ffb41ce7",
   "metadata": {},
   "outputs": [],
   "source": [
    "e = np.array([\"i\",\"love\",\"python\"],dtype = np.string_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f1a32bf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([b'i', b'love', b'python'], dtype='|S6')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b67bee1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
